﻿using Dapper;
using OMG.Fill.Frunit.Model;
using OMG.Fill.Frunit.Model.Ent;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.Linq;
namespace OMG.Fill.Frunit.Services
{
    /// <summary>
    /// 菜单操作权限表T_Menu_Action通用操作类
    /// </summary>
    public class MenuActionServices:MenuActions
    {
        public Pager<List<MenuActionEntity>> GetPageList(MenuActionQuery query)
        {
            Pager<List<MenuActionEntity>> obj = new Pager<List<MenuActionEntity>>();
            var paramlist = new DynamicParameters();
            string strWhereSql = "";//from 表在下面，需完善其他查询条件在这里接的写，格式为："and xxx=@xxx空格,如下注释部分代码所示："
            //if (!string.IsNullOrEmpty(query.GroupCode))
            //{
            //  strWhereSql += "and GroupCode = @GroupCode ";
            //  paramlist.Add("GroupCode", query.GroupCode);
            //}
            string strFromSql = "from dbo.T_Menu_Action where 1=1 " + strWhereSql;
            string strTotal = "select count(1)" + strFromSql + strWhereSql;
            string strOrderSql = "";//自定义完成order by语句
            string strSql = @"select * from (select *,ROW_NUMBER() over (order by Id ) as RowNumber " + strFromSql + ") as t ";
            strSql += "where t.RowNumber between  @minRow and @maxRow" + strOrderSql;
            paramlist.Add("minRow", query.MinRow);
            paramlist.Add("maxRow", query.MaxRow);
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    obj.rows = dbCon.Query<MenuActionEntity>(strSql,paramlist).ToList();
                    obj.total = dbCon.Query<int>(strTotal,paramlist).FirstOrDefault();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("获取菜单操作权限表T_Menu_Action分页列表失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 获取指定菜单的操作列表
        /// </summary>
        /// <param name="id">菜单编号</param>
        /// <param name="edittype">是否是编辑：不为空，是获取状态为启用的列表</param>
        /// <returns></returns>
        public ResResult<List<MenuActionEntity>> GetList(string id,string edittype)
        {
            ResResult<List<MenuActionEntity>> obj = new ResResult<List<MenuActionEntity>>();
            string strSql = @"select * from dbo.T_Menu_Action where MenuId=@MenuId";
            if (!string.IsNullOrEmpty(edittype)) {
                strSql += " and status='1' ";
            }
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    obj.model = dbCon.Query<MenuActionEntity>(strSql,new { MenuId=id }).ToList();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("更新菜单操作权限表T_Menu_Action对象信息失败", ex);
                return obj;
            }
        }
        public ResResult<int> AddInfo(MenuActionModel model)
        {
            ResResult<int> obj = new ResResult<int>();
            string strActionDelSql = @"Delete from dbo.T_Menu_Action where MenuId=@MenuId";
            string strActionSql = @"INSERT INTO dbo.T_Menu_Action";
            strActionSql += "(";
            strActionSql += "Id,";
            strActionSql += "MenuId,";
            strActionSql += "CtrlUrl,";
            strActionSql += "ActionName,";
            strActionSql += "ActionCode,";
            strActionSql += "Sort,";
            strActionSql += "Status,";
            strActionSql += "Remark)";
            strActionSql += " values(";
            strActionSql += "@Id,";
            strActionSql += "@MenuId,";
            strActionSql += "@CtrlUrl,";
            strActionSql += "@ActionName,";
            strActionSql += "@ActionCode,";
            strActionSql += "@Sort,";
            strActionSql += "@Status,";
            strActionSql += "@Remark)";
            using (var dbCon = ConnectFactory.GetOpenConnection())
            {
                var trans = dbCon.BeginTransaction();
                try
                {
                    dbCon.Execute(strActionDelSql, new { MenuId = model.MenuId }, trans);
                    if (model.ActionList.Count != 0)
                    {
                        foreach (var item in model.ActionList)
                        {
                            item.Id = CustomFun.GetGuid();
                            item.MenuId = model.MenuId;
                        }
                        dbCon.Execute(strActionSql, model.ActionList, trans);
                    }
                    //提交事务
                    trans.Commit();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "添加成功";
                    dbCon.Dispose();
                    return obj;
                }
                catch (Exception ex)
                {
                    obj.status = DefaultKey.ErrorResult;
                    obj.message = ex.ToString().Trim();
                    LogHelper.ErrorLog("添加失败", ex);
                    trans.Rollback();
                    dbCon.Dispose();
                    return obj;
                }
            }
        }
    }
}